/*
 * =====================================================================================
 *
 *       Filename:  libString.c
 *
 *    Description:  String library.
 *
 *        Version:  0.1
 *        Created:  2017/02/12 20:07:31
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Brook L. (), Brook_L@163.com
 *   Organization:  
 *
 * =====================================================================================
 */

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "libString.h"

/**
 * strToLower - convert a string to lowercase
 * @str: string to be converted
 *
 * Convert all letters of a string to lowercase.
 */
void strToLower(char *str) {
    while (*str != '\0') {
        *str = (char)tolower((int)(*str));
        str++;
    }
}

/*
 * strSplitInTwoParts - split a string in two parts, using specified dilimiter
 * @str: string to be splitted, the actual argument must be char array, but not char pointer!
 * @delim: delimiter
 * @head: the first part of the splitted string, char pointer's pointer
 * @tail: the last part of the splitted string, char pointer's pointer
 *
 * return result of the process:
 *      0  - success, either head or tail have content
 *      -1 - fail, neither head nor tail have content
 */
static int strSplitInTwoParts(char *str, char *delim, char **head, char **tail) {
    *head = strtok(str, delim);
    *tail = strtok(NULL, "");

    // judge if token is over
    if ((*head == NULL || strlen(*head) == 0) && (*tail == NULL || strlen(*head) == 0)) {
        return -1;
    } else {
        return 0;
    }
}

/**
 * strSplitInTwoValidPart - split a string in two valid parts, using specified dilimiter
 * @str: string to be splitted, the actual argument must be char array, but not char pointer!
 * @delim: delimiter
 * @head: the first part of the splitted string, char pointer's pointer
 * @tail: the last part of the splitted string, char pointer's pointer
 *
 * return result of the process:
 *      0  - valid head string found
 *      -1 - split over, and valid head string not found
 */
int strSplitInTwoValidPart(char *str, char *delim, char **head, char **tail) {
    *tail = str;
    while (*head == NULL || strlen(*head) == 0) {
        // split over, and *head is invalid
        if (strSplitInTwoParts(*tail, delim, head, tail) == -1) {
            return -1;
        }
    }

    return 0;
}

/**
 * strRemoveDelimiter - Remove all delimiters in a string
 * @str: string to be processed, the actual argument must be char array, but not char pointer!
 * @delim: delimiter
 * @result: string to save the result, the actual argument must be char array, but not char pointer!
 *
 * return the result 
 */
char *strRemoveDelimiter(char *str, char *delim, char *result) {
    char *head = NULL;
    char *tail = str;
    while (strSplitInTwoValidPart(tail, delim, &head, &tail) == 0) {
        strcat(result, head);
        head = NULL;                            // must set it to NULL
    }

    return result;
}

//int main(int argc, char *argv[]) {
//    char str[] = "1, 2,.*3 ()4 ```5$$$6##7,.8   9!!!";
//    char delim[] = " ,.()$#*`!";
//    char result[32] = {0};
//
//    printf("%s\n", strRemoveDelimiter(str, delim, result));
//
//    return 0;
//}
